package com.system.service;

import com.system.entity.Course;
import com.system.entity.DataException;
import com.system.util.FileUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 课程服务类
 * 处理课程信息的增删改查
 */
public class CourseService {
    private static final String COURSE_FILE = "course.txt";

    /**
     * 添加课程
     * @param course 课程对象
     * @throws DataException 数据异常
     */
    public void addCourse(Course course) throws DataException {
        // 验证学分
        if (course.getCredit() < 0.5 || course.getCredit() > 5.0) {
            throw new DataException("学分必须在0.5-5.0之间");
        }

        Course[] courses = FileUtil.readFromFile(COURSE_FILE, Course.class);
        
        // 检查课程号是否重复
        for (Course c : courses) {
            if (c.getCourseId().equals(course.getCourseId())) {
                throw new DataException("课程号已存在");
            }
        }

        // 添加新课程
        List<Course> courseList = new ArrayList<>();
        for (Course c : courses) {
            courseList.add(c);
        }
        courseList.add(course);

        FileUtil.writeToFile(COURSE_FILE, courseList.toArray(new Course[0]));
    }

    /**
     * 删除课程
     * @param courseId 课程号
     * @throws DataException 数据异常
     */
    public void deleteCourse(String courseId) throws DataException {
        Course[] courses = FileUtil.readFromFile(COURSE_FILE, Course.class);
        List<Course> courseList = new ArrayList<>();
        boolean found = false;

        for (Course course : courses) {
            if (!course.getCourseId().equals(courseId)) {
                courseList.add(course);
            } else {
                found = true;
            }
        }

        if (!found) {
            throw new DataException("该课程号不存在");
        }

        FileUtil.writeToFile(COURSE_FILE, courseList.toArray(new Course[0]));
    }

    /**
     * 修改课程信息
     * @param course 课程对象
     * @throws DataException 数据异常
     */
    public void updateCourse(Course course) throws DataException {
        // 验证学分
        if (course.getCredit() < 0.5 || course.getCredit() > 5.0) {
            throw new DataException("学分必须在0.5-5.0之间");
        }

        Course[] courses = FileUtil.readFromFile(COURSE_FILE, Course.class);
        List<Course> courseList = new ArrayList<>();
        boolean found = false;

        for (Course c : courses) {
            if (c.getCourseId().equals(course.getCourseId())) {
                courseList.add(course);
                found = true;
            } else {
                courseList.add(c);
            }
        }

        if (!found) {
            throw new DataException("该课程号不存在");
        }

        FileUtil.writeToFile(COURSE_FILE, courseList.toArray(new Course[0]));
    }

    /**
     * 按课程号查询课程
     * @param courseId 课程号
     * @return 课程对象
     * @throws DataException 数据异常
     */
    public Course getCourseById(String courseId) throws DataException {
        Course[] courses = FileUtil.readFromFile(COURSE_FILE, Course.class);

        for (Course course : courses) {
            if (course.getCourseId().equals(courseId)) {
                return course;
            }
        }

        throw new DataException("该课程号不存在");
    }

    /**
     * 获取所有课程（按课程号升序）
     * @return 课程数组
     */
    public Course[] getAllCourses() {
        Course[] courses = FileUtil.readFromFile(COURSE_FILE, Course.class);
        // 按课程号升序排序
        Arrays.sort(courses, Comparator.comparing(Course::getCourseId));
        return courses;
    }

    /**
     * 判断课程是否存在
     * @param courseId 课程号
     * @return 是否存在
     */
    public boolean isCourseExists(String courseId) {
        try {
            getCourseById(courseId);
            return true;
        } catch (DataException e) {
            return false;
        }
    }
}